package com.example.heimashop.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.heimashop.databinding.ItemAddressBinding;
import com.example.heimashop.model.Address;

import java.util.ArrayList;
import java.util.List;

/**
 * 地址列表适配器
 */
public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.ViewHolder> {

    private List<Address> addresses = new ArrayList<>();
    private OnAddressItemListener listener;

    public interface OnAddressItemListener {
        void onItemClick(Address address);
        void onSetDefault(Address address);
        void onEdit(Address address);
        void onDelete(Address address);
    }

    public void setListener(OnAddressItemListener listener) {
        this.listener = listener;
    }

    public void setData(List<Address> list) {
        if (list != null) {
            this.addresses = list;
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemAddressBinding binding = ItemAddressBinding.inflate(
                LayoutInflater.from(parent.getContext()),
                parent,
                false
        );
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Address address = addresses.get(position);
        holder.bind(address, listener);
    }

    @Override
    public int getItemCount() {
        return addresses.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final ItemAddressBinding binding;

        ViewHolder(ItemAddressBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(Address address, OnAddressItemListener listener) {
            // 设置整个卡片的点击监听
            binding.getRoot().setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemClick(address);
                }
            });

            // 设置收货人和电话
            binding.tvConsignee.setText(address.getConsignee());
            binding.tvPhone.setText(address.getPhone());

            // 设置完整地址
            binding.tvAddress.setText(address.getFullAddress());

            // 显示/隐藏默认标签
            if (address.isDefault()) {
                binding.tvDefault.setVisibility(View.VISIBLE);
                binding.btnSetDefault.setVisibility(View.GONE);
            } else {
                binding.tvDefault.setVisibility(View.GONE);
                binding.btnSetDefault.setVisibility(View.VISIBLE);
            }

            // 设置点击监听
            binding.btnSetDefault.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onSetDefault(address);
                }
            });

            binding.btnEdit.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onEdit(address);
                }
            });

            binding.btnDelete.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onDelete(address);
                }
            });
        }
    }
}
